// 对模型添加全局类方法
const classExtend = {
    extendModel: function(model, attr, modelExt) {
        // 判断name是否一个定义的属性列
        model.isDefineAtt = function(name) {
            return attr[name] != null;
        };
        // 获得一个定义的属性列
        model.getDefineAtt = function(name) {
            return attr[name];
        };

        // 获得可以修改的属性列
        model.getUpdateFields = function() {
            const fields = {
                'fields': modelExt.updates
            };
            return fields;
        };
        // 获得引用属性列
        model.getReferences = function() {
            if (modelExt.references.length == 0) {
                return null;
            }
            var refArr = [];
            for (var index = 0; index < modelExt.references.length; index++) {
                //console.log(modelExt.references[index].model);
                refArr.push(modelExt.references[index].model);
            }
            return refArr;
        };
        // 获得完整的引用属性列
        model.getFullReferences = function(isIncludeMany) {
            // if (modelExt.references.length == 0) {
            //     return null;
            // }
            var refArr = [];
            if(isIncludeMany == true && model.getHasMany) {
                var arrAsName = model.getHasMany();

                if(arrAsName != null && arrAsName.length > 0){
                    refArr = arrAsName;
                }
                console.log(refArr);
            }
            
            for (var index = 0; index < modelExt.references.length; index++) {
                // var refObj = {
                //     model: model.associations[modelExt.references[index].model].target,
                //     as: modelExt.references[index].model
                // }
                refArr.push(modelExt.references[index].model);
            }
            
            return refArr;
        };
        model.setPropertys = function(sysName, obj) {
            obj.lastUpdatedDate = new Date();
            obj.modifier = sysName;
            obj.creator = sysName;
            obj.cretaeDate = new Date();
        };
        // 查询一个对象是否存在，存在返回一个新的数据库实体对象
        model.findByObjCode = async function(obj) {
            if (obj == null) {
                return null;
            }
            var refs = model.getReferences();
            const queryCondition = {
                "where": {},
                "include": refs
            }
            for (var i = 0; i < modelExt.akNameArray.length; i++) {
                queryCondition.where[modelExt.akNameArray[i]] = obj[modelExt.akNameArray[i]];
            }
            console.log(queryCondition);
            return model.findOne(queryCondition);
        };
        // 根据唯一编码来获得一个对象
        model.findByCode = async function(...codes) {
            if (codes == null || codes.length <= 0) {
                return null;
            }
            if (modelExt.akNameArray.length != codes.length) {
                return null;
            }
            // 组装where子句
            //console.log(akNameArray);
            var refs = model.getReferences();
            const queryCondition = {
                "where": {},
                "include": refs
            }
            for (var i = 0; i < modelExt.akNameArray.length; i++) {
                queryCondition.where[modelExt.akNameArray[i]] = codes[i];
            }
            //console.log('model.findOne');
            return model.findOne(queryCondition);
            //return 1;

        };

        model.akNameArray = function() {
            return modelExt.akNameArray;
        };
        // 根据唯一编码来判断对象是否存在
        model.isExists = async function(...codes) {
            if (codes == null || codes.length <= 0) {
                return null;
            }
            if (modelExt.akNameArray.length != codes.length) {
                return null;
            }
            // 组装where子句
            //console.log(akNameArray);
            const queryCondition = {
                "where": {}
            }
            for (var i = 0; i < modelExt.akNameArray.length; i++) {
                queryCondition.where[modelExt.akNameArray[i]] = codes[i];
            }
            return model.count(queryCondition);
        };
    }

};


module.exports = classExtend;